package com.systop.controller;

import com.systop.pojo.Goods;
import com.systop.redis.RedisUntil;
import com.systop.service.GoodsService;
import com.systop.unti.ResponseResult;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisUntil redisUntil;
    @RequestMapping(value = "/addGoods")
    public ResponseResult addGoods(@RequestParam("goods_user_id") Integer goods_user_id, @RequestParam("goods_status_id") Integer goods_status_id,
                                   @RequestParam("goods_property_id") Integer goods_property_id, @RequestParam("goods_type_id") Integer goods_type_id,
                                   @RequestParam("goods_name") String goods_name, @RequestParam("goods_price") String goods_price,
                                   @RequestParam(value = "file", required = false) MultipartFile[] file, @RequestParam("goods_describtion") String goods_describtion,
                                   @RequestParam("remark") String remark, @RequestParam("goods_times") String goods_times) throws IOException, JSONException {
        JSONObject res = new JSONObject();
        JSONObject resUrl = new JSONObject();
        List<String> imageurls=new ArrayList<>();
        for (MultipartFile files:file){
            String filename = UUID.randomUUID().toString().replaceAll("-", "");
            String ext = FilenameUtils.getExtension(files.getOriginalFilename());
            String filenames = filename + "." + ext;
            System.out.println(filenames);
            String pathname = "D:\\workspeace\\hgtt\\user\\src\\main\\resources\\goodsPic\\" + filenames;
            files.transferTo(new File(pathname));
            imageurls.add("goodsPic/"+filenames);
        }
        resUrl.put("src", imageurls);
        res.put("data", resUrl);
        String goods_photo = resUrl.get("src").toString();
        System.out.println(goods_photo);
        System.out.println(resUrl.get("src"));
        System.out.println(res.get("data"));
        Goods goods = new Goods();
        goods.setGoods_user_id(goods_user_id);
        goods.setGoods_status_id(goods_status_id);
        goods.setGoods_property_id(goods_property_id);
        goods.setGoods_type_id(goods_type_id);
        goods.setGoods_name(goods_name);
        goods.setGoods_price(goods_price);
        goods.setGoods_photo(goods_photo);
        goods.setGoods_describtion(goods_describtion);
        goods.setRemark(remark);
        goods.setGoods_times(goods_times);
        redisUntil.del("goods");
        return goodsService.addGoods(goods);
    }
    @RequestMapping(value = "/findAllGoods")
    public ResponseResult findall(HttpSession session){
        if (redisUntil.lGetListSize("goods") == 0){
            List<Goods> list = goodsService.findall();
            session.setAttribute("list", list);
            redisUntil.lSet("goods",list);
            return ResponseResult.ok(list);
        }
        System.out.println(ResponseResult.ok(redisUntil.lGet("goods",0,-1)).getData());
        return ResponseResult.ok(redisUntil.lGet("goods",0,-1));

    }
    @RequestMapping(value = "/testaaa")
    public long testaaa(){
        return redisUntil.lGetListSize("goods");
    }

}
